package jc.malpractice.controllers;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import jc.malpractice.service.MyeventSerice;
import spark.Route;

import java.text.SimpleDateFormat;
import java.util.Date;

import static spark.Spark.*;

public class MyeventController {


    static Route select = (request, response) ->
    {
        String name=request.raw().getParameter("name");
        return MyeventSerice.list(name);
    };

    static Route select_rough = (request, response) ->
    {
        String name=request.raw().getParameter("name");
        String event_code=request.raw().getParameter("event_code");
        System.out.println(event_code);
        return MyeventSerice.selectRough(name,event_code);
    };

    static Route handle_event = (request, response) ->
    {
        String dept_name=request.raw().getParameter("dept_name");
        return MyeventSerice.selectDeptData(dept_name);
    };

    static  Route partselect = (request, response) ->
    {
        String name =request.raw().getParameter("name");
        String event_code=request.raw().getParameter("event_code");
        String status = request.raw().getParameter("status");
        String dept_name = request.raw().getParameter("dept_name");
        String report_date = request.raw().getParameter("report_date");
        String reporter = request.raw().getParameter("reporter");
        return MyeventSerice.partselect(name,event_code,status,dept_name,report_date,reporter);
    };
    static  Route handle_partselect = (request, response) ->
    {
        String name =request.raw().getParameter("name");
        String status = request.raw().getParameter("status");
        String kind_name = request.raw().getParameter("kind_name");
        System.out.println(name);
        System.out.println(status);
        System.out.println(kind_name);
        return MyeventSerice.handleSelect(name,status,kind_name);
    };

    static Route event_insert = (req, res) -> {
        String reporter_code = req.queryParams("reporter_code");
        String reporter_dept = req.queryParams("reporter_department");
        String handler_code = "ys0403";
        String event_code = req.queryParams("event_code");
        String form_code = String.valueOf(System.currentTimeMillis());
        String status = req.queryParams("status");
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter.format(date));
        String event_time = formatter.format(date);

        //提交到总表
        JSONObject jo = MyeventSerice.insertEventAll(form_code,reporter_code,reporter_dept,handler_code,event_code,event_time,status);
        //获取所有数据列
        JSONArray property = MyeventSerice.selectAllProperty().getJSONArray("data");
        int property_length = property.toArray().length;
        JSONObject demo = null;
        String detailed_data = null;
        for(int i=0; i<property_length; i++) {
            demo = property.getJSONObject(i);
            detailed_data = req.queryParams(demo.getStr("property_en_name"));
            if(detailed_data != null) {
                MyeventSerice.insertEventDetailed(form_code,demo.getStr("property_code"),detailed_data);
                demo = null;
                detailed_data = null;
            }
        }

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "数据提交失败");
        }

        return jo;
    };

    static Route event_update = (req, res) -> {
        String form_code = req.queryParams("form_code");
        String status = req.queryParams("status");
        JSONObject jo = null;

        MyeventSerice.updateEventStatus(status,form_code);

        //获取所有数据列
        JSONArray property = MyeventSerice.selectAllProperty().getJSONArray("data");
        int property_length = property.toArray().length;
        JSONObject demo = null;
        String detailed_data = null;
        for(int i=0; i<property_length; i++) {
            demo = property.getJSONObject(i);
            detailed_data = req.queryParams(demo.getStr("property_en_name"));
            if(detailed_data != null) {
                jo = MyeventSerice.updateEventDetailed(form_code,demo.getStr("property_code"),detailed_data);
                demo = null;
                detailed_data = null;
            }
        }

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "数据修改失败");
        }

        return jo;
    };

    static Route event_handle = (req, res) -> {
        String form_code = req.queryParams("form_code");
        String reporter_dept = req.queryParams("reporter_dept");
        String suggestion = req.queryParams("suggestion");
        JSONObject jo = null;

        jo = MyeventSerice.handleEvent(form_code,suggestion,reporter_dept);

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "数据修改失败");
        }

        return jo;
    };
    //病人信息搜索
    static Route event_patient = (req, res) -> {
        String in_his_no = req.queryParams("in_his_no");
        JSONObject jo = null;

        jo = MyeventSerice.selectPatient(in_his_no);

        if (jo == null) {
            jo = new JSONObject();
            // pac_ward 为空或异常
            jo.set("code", 1001);
            jo.set("msg", "数据查询失败");
        }

        return jo;
    };

    static Route look = (request, response) -> {
        String form_code = request.raw().getParameter("form_code");
        return MyeventSerice.look(form_code);
    };

    public static void Init() {
        get("/Zz/My_event", MyeventController.select);
        get("/Zz/handle_event", MyeventController.handle_event);
        get("/Zz/My_event/select",MyeventController.partselect);
        get("/Zz/My_event/hander_select",MyeventController.handle_partselect);
        get("/Zz/My_event/selectRough", MyeventController.select_rough);
        post("/event/event_insert", event_insert);
        post("/event/event_update", event_update);
        post("/event/event_handle", event_handle);
        post("/event/event_patient", event_patient);
        get("/look",MyeventController.look);
    }


}
